<?php

namespace app\common\model;

use think\Model;

class Order extends Model
{
    static $status = [1=>'异常待处理',2=>'异常处理中',3=>'受理完成'];
    static $level = ['1'=>'轻','2'=>'一般',3=>'严重' ];
    static $withWhere = ['ins_id','display','ins_name','inputtime1','status','number','equ_id','accept','culvertNo','centerDistance'];
    static function addOne($info){
        $info['display'] = 1;

        return self::insertGetId($info);
    }

    static function edit($id,$info){
        return self::where(['id'=>$id,'display'=>1])->update($info);
    }

    static function getList($page,$pageSize,$search,$order = [],$field=['a.id','inputtime1','inputtime2','inputtime3','a.status','equ_id','video','accept','img_scene','img_car','level','car_id','a.name','id_card','a.phone','note','a.ins_id','ins_name','number','staff.name as staff_name','equipment.name as equ_name','equipment.address as equ_add','culvertNo','centerDistance']){
        $currentPage = $page;
        $page = ($page - 1) * $pageSize;
        $where = self::$withWhere;
        $info = self::field($field)->alias('a')
            ->withSearch($where,$search )
            ->join('staff','a.accept = staff.id','LEFT')
            ->join('equipment','a.equ_id = equipment.id','LEFT')
            ->order($order)
            ->limit($page,$pageSize)
            ->select();
        $count = self::alias('a')->withSearch($where,$search )->count();
        foreach ($info as $infoKey => $infoValue){
            $info[$infoKey]['do_status'] = self::$status[$infoValue['status']];
            $info[$infoKey]['inputtime1'] =  $info[$infoKey]['inputtime1']?date('Y-m-d H:i',$info[$infoKey]['inputtime1']):'--';
            $info[$infoKey]['inputtime2'] = $info[$infoKey]['inputtime2']?date('Y-m-d H:i',$info[$infoKey]['inputtime2']):'--';
            $info[$infoKey]['inputtime3'] = $info[$infoKey]['inputtime3']?date('Y-m-d H:i',$info[$infoKey]['inputtime3']):'--';
            if(!$info[$infoKey]['ins_name']){
                $ins = model('common/Institution')->get_one($info[$infoKey]['ins_id']);
                $info[$infoKey]['ins_name'] = $ins['name'];
            }
            $camera = model('Common/EqRelation')->get_ong($info[$infoKey]['equ_id']);
//            $camera = model('common/Equipment')->getType1($info[$infoKey]['ins_id']);
            $video = self::getVideo($info[$infoKey]['id'],$info[$infoKey]['video'],$camera['code'],$info[$infoKey]['inputtime1']);
            $info[$infoKey]['video'] = $video;
        }
        $arr =  ['list'=>$info,'count'=>$count,'page'=>$currentPage,'pageSize'=>$pageSize];
        return $arr;
    }
    //机构名称搜索
    public function searchInsNameAttr($query,$value, $data)
    {
        if($value){
            $query->where('ins_name','like', '%' .$value . '%');
        }

    }
    //时间搜索
    public function searchInputTime1Attr($query,$value, $data)
    {
        if($value){
            $query->whereBetweenTime('inputtime1', $value[0], $value[1]);
        }
    }
    //状态搜索
    public function searchStatusAttr($query,$value, $data)
    {
        if($value){
            $query->where('a.status', '=', $value);
        }
    }
    //用户ID搜索
    public function searchAcceptAttr($query,$value, $data)
    {
        if($value){
            $query->where('a.accept', '=', $value);
        }
    }
    //设备id 搜索
    public function searchEquIdAttr($query,$value, $data)
    {
        if($value){
            $query->where('equ_id', '=', $value);
        }
    }
    //编号搜索
    public function searchNumberAttr($query,$value, $data)
    {
        if($value){
            $query->where('number', 'like',"%".$value."%");
        }
    }
    public function searchDisplayAttr($query,$value, $data)
    {
        if($value){
            $query->where('a.display', '=', $value);
        }else{
            $query->where('a.display', '=', 1);
        }
    }
    //机构id查询
    public function searchInsIdAttr($query,$value, $data)
    {
        if($value){
            $query->where('ins_id', '=', $value);
        }
    }
    public function searchCulvertNoAttr($query,$value, $data)
    {
        if($value){
            $query->where('culvertNo', '=', $value);
        }
    }
    public function searchCenterDistanceAttr($query,$value, $data)
    {
        if($value){
            $query->where('centerDistance', '=', $value);
        }
    }

    //根据权限 机构 状态  查个数
    //index 1 前台
    public function count_status($authority,$ins_id,$status,$user_id,$index=0){
        $where = self::powerWhere($authority,$ins_id,$status,$user_id,$index);
        return self::alias('a')->withSearch(self::$withWhere,$where )->count();
        return $this->where($where)->count();
    }

    static public function  powerWhere($authority,$ins_id,$status,$user_id,$index=0)
    {
        $where['display'] = 1;
        if($authority == 1){
            $where['status'] = $status;
        }else {
            $where['status'] = $status;
            $where['ins_id'] = $ins_id;
        }
        if(!$index){
            switch($status){
                case 1:
                    break;
                case 2:
                    $where['accept'] = $user_id;
                    break;
                case 3:
                    $where['accept'] = $user_id;
                    break;
            }

        }
        return $where;
    }

    public function get_one($id){
        $info = $this->where(['id'=>$id,'display'=>1])->find();
        $info['inputtime1'] = $info['inputtime1']?date('Y-m-d H:i',$info['inputtime1']):'--';
        $info['inputtime2'] = $info['inputtime2']?date('Y-m-d H:i',$info['inputtime2']):'--';
        $info['inputtime3'] = $info['inputtime3']?date('Y-m-d H:i',$info['inputtime3']):'--';
        $info['do_img'] = $info['img_scene'];
        $info['do_status'] = self::$status[$info['status']];
        $img_scene =json_decode($info['img_scene'],true);
        $info['img_scene'] = '';
        if(!empty($img_scene)){
            $info['img_scene'] = explode(',', $img_scene[0]) ? explode(',', $img_scene[0]) :  $img_scene;
        }
//
	    $img_car = json_decode($info['img_car'],true);
        $info['img_car'] = '';
        if(!empty($img_car)){
            $info['img_car'] = explode(',', $img_car[0]) ? explode(',', $img_car[0]) :  $img_car;
        }
        $info['do_level'] = '--';
        if($info['level']) $info['do_level'] = self::$level[$info['level']];
//        $camera = model('common/Equipment')->getType1($info['ins_id']);
        $camera = model('Common/EqRelation')->get_ong($info['equ_id']);
        $video = self::getVideo($info['id'],$info['video'],$camera['code'],$info['inputtime1']);
        $info['video'] = $video;
        return $info;
    }

    static function getVideo($id,$Video,$uid,$time){
        if(!$uid) return $Video;
       // if($Video) return $Video;
	    $time = strtotime($time) ? strtotime($time) : $time;
	
        $Videos = Video::getTimeMp4($uid,$time);
        if(!$Videos) return $Video;
        //$info['video'] = $Videos;
        //self::where(['id'=>$id,'display'=>1])->update($info);
        return $Videos;
    }

}
